<template>
	<view class="love-main">
		<!-- 首页头部恋爱时间以及我们相册轮播 @click.stop="changeLoveBanner()"-->
		<view class="love-time"
			:style="'background-image:url(' + getLoveStartBanners + ');height: ' + (isTodayend ? 60 : 100) + 'vh;'">
			<LoveTime></LoveTime>
			<LoveBtnFun class="love-btn" v-if="!isTodayend"></LoveBtnFun>
		</view>
		<LoveFour class="love-four" ref="loveFourRef"></LoveFour>
		<view class="story">
			<item-header title="故事" navigatorUrl="/pages/story/index" margin="20"></item-header>
			<story v-for="(item, index) in storys" :key="index" :story="item"></story>
		</view>

		<u-divider height="200">到心底啦~</u-divider>
		<push-popup ref="addStoryPopup"></push-popup>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import LoveTime from './components/love-time.vue';
	import LoveBtnFun from './components/love-btn-fun.vue';
	import LoveFour from './components/love-four.vue';
	import {
		mapGetters,
		mapState,
		mapMutations
	} from 'vuex';
	import {
		DateUtil,
		format
	} from '../../common/DateUtil';
	export default {
		components: {
			LoveTime,
			LoveBtnFun,
			LoveFour
		},
		data() {
			return {
				storys: [],
				misss: [{
					messageContent: '',
					userBase: {
						nickname: ''
					}
				}],
				canIGetUserProfile: false,
				isNewUser: false,
				bannerHeight: 100
			};
		},
		computed: {
			...mapGetters(['getLoveStartId', 'getLoveStartInfo', 'getLoveStartBanners', 'getIsLogin', 'getLoginUser',
				'isTodayend'
			])
		},
		onLoad() {
			if (this.getLoveStartId != 0) {
				this.init();

			}
		},
		onPullDownRefresh() {
			if (this.getLoveStartId != 0) {
				this.init(true);
			}
		},
		methods: {
			...mapMutations(['modifyLoveStartInfo', 'modifyLastSendDate']),
			init(isRefresh = false) {

				//查询恋爱空间信息
				this.$u.api
					.queryLoveStart({
						loveId: this.getLoveStartId
					})
					.then(res => {
						// console.info(res);
						this.modifyLoveStartInfo(res);
					});
				//查询思念留言
				this.$u.api
					.queryMessageList({
						loveId: this.getLoveStartId,
						messageType: 1,
						pageSize: 1,
						todayMessage: true,
						userId: this.getLoginUser.id
					})
					.then(res => {
						// console.info(res);
						if (res.data.length > 0) {
							this.modifyLastSendDate(new Date(res.data[0].createTime));
						}
					});
				//查询故事
				this.$u.api
					.queryStoryList({
						loveId: this.getLoveStartId
					})
					.then(res => {
						// console.info(res);
						this.storys = res.data;
						uni.stopPullDownRefresh();
					});
				if (isRefresh) {
					this.$refs.loveFourRef.init();
				}
			},
			//跳转更多信息页面
			moreInfo: function(currId) {
				uni.switchTab({
					url: '/pages/memories/memories',
					success() {
						uni.setStorageSync('memoriesCurrent', currId);
					}
				});
			},
			changeLoveBanner() {
				let that = this;
				uni.showActionSheet({
					itemList: ['修改壁纸'],
					success: function(res) {
						if (res.tapIndex == 0) {
							that.changeRoomWallpaper();
						}
					}
				});
			},
			changeRoomWallpaper() {
				let that = this;
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					sourceType: ['album'],
					success: function(res) {
						// console.log(res.tempFilePaths);
						that.uploadBanner(res.tempFilePaths);
					}
				});
			},
			uploadBanner(tempFilePaths) {
				let that = this;
				uni.uploadFile({
					url: that.$pig.constData.uploadUrl,
					filePath: tempFilePaths[0],
					name: 'file',
					formData: {
						loveId: that.getLoveStartId,
						entityId: that.getLoveStartId,
						entityType: 2
					},
					success: uploadFileRes => {
						// console.log(uploadFileRes.data);
						//查询恋爱空间信息
						that.$u.api
							.queryLoveStart({
								loveId: that.getLoveStartId
							})
							.then(res => {
								// console.info(res);
								that.loveStart = res;
								that.modifyLoveStartInfo(res);
							});
						that.$refs.uToast.show({
							title: '修改成功',
							type: 'success'
						});
					}
				});
			}
		}
	};
</script>

<style lang="scss">
	.love-time {
		height: 100vh;
		width: 100vw;
		display: flex;
		align-items: center;
		flex-direction: column;
		padding-top: $love-padding;
		background-repeat: no-repeat;
		background-size: cover;
		background-position: center;
		background-color: rgba($love-color, 0.3);
		transition: all 1s;
		position: relative;
	}

	.love-btn {
		position: absolute;
		bottom: 20%;
	}

	.story {
		// margin: 0 $love-margin;
		// margin-left: $love-view-margin;
		// margin-right: $love-view-margin;
	}
</style>